import Base from '../../static/js/base'
class Cart extends Base{
	constructor() {
		super();
	}
	// 购物车列表
	cart_list(callBack) {
		var that = this;
		var params = {
		  url: 'cart/cart_list',
		  sCallBack: function (res) {
			callBack && callBack(res.data);
		  }
		};
		that.request(params);
	};
	//购物车的加减
	change(cart_that,value,index){
		var that = this;
		var url = '';
		if(cart_that.list[index].num > value){
			url = 'cart/cart_subtract';
		}else{
			url = 'cart/cart_add';
		}
		//
		var params = {
		  url: url,
		  method:'PUT',
		  data:{
			id:cart_that.list[index].id  
		  },
		  sCallBack: function (res) {
			 if(res.data.code == 8888){
				 cart_that.list[index].on = true;
				 cart_that.list[index].num = value;
				 //计算购物车
				 that.count_cart(cart_that);
			 }	
		  }
		};
		that.request(params);
	}
	//购物车商品删除
	cart_delete(id,callBack) {
		var that = this;
		var params = {
		  url: 'cart/cart_delete',
		  method:'DELETE',
		  data:{
			id:id  
		  },
		  sCallBack: function (res) {
			callBack && callBack(res.data);
		  }
		};
		that.request(params);
	};
	count_cart(cart_that){
		var index = 0;
		var total_num = 0;
		var total_money = 0.00;
		for(var i=0;i<cart_that.list.length;i++){
			if(cart_that.list[i].on){
				index++;
				total_num += cart_that.list[i].num;
				total_money += cart_that.list[i].num * cart_that.list[i].total_money; 
			}
		}
		if(cart_that.list.length == index){
			cart_that.on_all = true;
		}else{
			cart_that.on_all = false;
		}
		cart_that.total_num = total_num;
		cart_that.total_money = total_money.toFixed(2);
	}
	all_select(cart_that){
		var total_num = 0;
		var total_money = 0.00;
		if(cart_that.on_all == false){
			cart_that.on_all = true;
		}else{
			cart_that.on_all = false;
		}
		for(var i=0;i<cart_that.list.length;i++){
			if(cart_that.on_all){
				cart_that.list[i].on = true;
				total_num += cart_that.list[i].num;
				total_money += cart_that.list[i].num * cart_that.list[i].total_money; 
			}else{
				cart_that.list[i].on = false;
			}
		}
		cart_that.total_num = total_num;
		cart_that.total_money = total_money.toFixed(2);
	}
	//获取收货地址
	get_user_site(callBack) {
		var that = this;
		var params = {
		  url: 'site/get_user_site',
		  sCallBack: function (res) {
			callBack && callBack(res.data);
		  }
		};
		that.request(params);
	};
	//获取收货地址
	add_cart_order(data,callBack) {
		var that = this;
		var params = {
		  url: 'order/add_cart_order',
		  data:data,
		  method:'POST',
		  sCallBack: function (res) {
			callBack && callBack(res.data);
		  }
		};
		that.request(params);
	};
	// 我的信息
	my(callBack) {
		var that = this;
		var params = {
		  url: 'login/my',
		  method:'POST',
		  sCallBack: function (res) {
			callBack && callBack(res.data);
		  }
		};
		that.request(params);
	};
}

export default Cart	